package com.pumpkin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pumpkin.server.commons.ResBean;
import com.pumpkin.server.pojo.RoleMenu;
import com.pumpkin.server.mapper.RoleMenuMapper;
import com.pumpkin.server.service.IRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pumpkin
 * @since 2022-07-13
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {


    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 更新角色菜单
     * @param rid
     * @param mids
     * @return
     */
    @Override
    @Transactional
    public ResBean updateMenuRole(Integer rid, Integer[] mids) {
        //先根据roleId删除关联表中所有的菜单id
        roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("roleId",rid));
        //再根据roleId添加菜单id（在此之前先判断传入的mid是否为空：为空的情况是没有菜单权限了）
        if (null == mids || 0 == mids.length) {
            return ResBean.success("更新成功！");
        }
        //批量更新
        Integer result = roleMenuMapper.insertRecord(rid, mids);
        if (result == mids.length) {
            return ResBean.success("更新成功！");
        }
        return ResBean.error("更新失败！");
    }
}
